package org.dtrd.modules.service.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.modules.service.entity.bean.PackageSpecs;
import org.dtrd.modules.service.entity.bean.ServicePackage;
import org.dtrd.modules.service.entity.po.DtrdEntRdServicePackageSpecs;
import org.dtrd.modules.service.mapper.DtrdEntRdServicePackageSpecsMapper;
import org.dtrd.modules.service.service.IDtrdEntRdServicePackageSpecsService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 服务包规格配置 服务实现类
 * </p>
 *
 * @author xiao
 * @since 2022-06-20
 */
@DS("multi-datasource1")
@Service
public class DtrdEntRdServicePackageSpecsServiceImpl extends ServiceImpl<DtrdEntRdServicePackageSpecsMapper, DtrdEntRdServicePackageSpecs> implements IDtrdEntRdServicePackageSpecsService {

    @Override
    public DtrdEntRdServicePackageSpecs getServicePackageSpecs(Integer packageId) {
        return baseMapper.getServicePackageSpecs(packageId);
    }

    @Override
    public ServicePackage getServicePackageSpecsInfo(Integer packageId, Integer packageDiscount) {
        DtrdEntRdServicePackageSpecs specs = baseMapper.getServicePackageSpecs(packageId);
        ServicePackage sp = null;
        if (specs != null) {
            sp = new ServicePackage();
            sp.setSaleState(specs.getPackageSaleState() == 1);
            String packageSpecs = specs.getPackageSpecs();
            List<PackageSpecs> specsList;
            if (StrUtil.isNotBlank(packageSpecs)) {
                specsList = JSON.parseArray(packageSpecs, PackageSpecs.class);
                specsList.stream().filter(spec -> spec.getPrice() == null).forEach(spec -> spec.setPrice(packageDiscount));
            } else {
                specsList = new ArrayList<>(1);
                specsList.add(new PackageSpecs("默认", packageDiscount));
            }
            sp.setPackageSpecs(specsList);
        }
        return sp;
    }
}
